<template>
    <view class="content">
		<view class="back">
			<m-icon type="arrowleft" size="28" class='btn-icon' @click="back()"></m-icon>
		</view>
		
        <view class="input-group">
			<view class="log-input">
				<image src="../../static/function/user.png" mode="widthFix" class="log-img"></image>
				<m-input class="input" type="text" v-model="realname" :placeholder="i18n.reg.username" maxlength="30"></m-input>
			</view>
			
			<!-- <view class="log-input">
				<image src="../../static/function/user.png" mode="widthFix" class="log-img"></image>
				<m-input class="input" type="text" v-model="engname" :placeholder="i18n.reg.englname"></m-input>
			</view> -->
			
            <view class="log-input">
				<image src="../../static/function/account.png" mode="widthFix" class="log-img"></image>
                <m-input class="input" type="text" v-model="account" :placeholder="i18n.login.mobile" maxlength="20"></m-input>
            </view>
			
            <view class="log-input">
				<image src="../../static/function/pass.png" mode="widthFix" class="log-img"></image>
                <m-input class="input" type="password" v-model="pwd" :placeholder="i18n.login.pass" maxlength="20"></m-input>
            </view>
			
            <view class="log-input">
				<image src="../../static/function/pass.png" mode="widthFix" class="log-img"></image>
                <m-input class="input" type="password" v-model="pwd2" :placeholder="i18n.reg.pass2" maxlength="6"></m-input>
            </view>
			
			<textarea v-model="wallet_address" :placeholder="i18n.reg.address" maxlength="45" class="textarea"></textarea>
			
			<view class="action-row">
				<block v-if="imageSrc">
					<image :src="imageSrc" class="image" mode="widthFix" @click="chooseImage"></image>
				</block>
				
				<block v-else>
					<view  class='img-src'>
						<view class="addfile" @click="chooseImage">
							{{i18n.reg.imgsrc}}
						</view>
					</view>
				</block>
			</view>
        </view>
		
        <view class="btn-row">
			<block v-if='status == 1'>
				<view type="primary" class="primary1" @tap="Register">
					{{i18n.user.regist}}
				</view>
			</block>
			
			<block v-else>
				<view type="primary" class="primary1">
					{{i18n.user.regist}}
				</view>
			</block>
        </view>
    </view>
</template>

<script>
    import service from '../../service.js';
    import mInput from '../../components/m-input.vue';
	import mIcon from '../../components/m-icon/m-icon.vue';

    export default {
		computed: {
			i18n () {
				return this.$t('index')
			}
		},
        components: {
            mInput,
			mIcon
        },
        data() {
            return {
				token:uni.getStorageSync('token'),
                providerList: [],
                hasProvider: false,
				realname:'',
				engname:'',
                account: '',
                pwd: '',
				pwd2:'',
				wallet_address:'',
				imageSrc:'',
				countdown:62,
				status:1,
                positionTop: 0
            }
        },
		onShow:function(){
			uni.setNavigationBarTitle({
				title: this.i18n.pages.help
			});
		},
        methods: {
			back(){
				uni.reLaunch({
					url: '/pages/main/user',
				});
			},
			//選擇圖片
			chooseImage: function() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album','camera'],
					success: (res) => {
						var imageSrc = res.tempFilePaths[0];
						this.imageSrc_list = imageSrc;
						this.imageSrc = imageSrc;
					},
					fail: (err) => {
						console.log('chooseImage fail', err)
					}
				})
			},
			Register(){
				self = this;
				var text = '';
				if(self.realname.length < 2){
					text = self.i18n.reg.characters;
				/* }else if(self.engname.length < 2){
					text = self.i18n.reg.english; */
				}else if(self.account == ''){
					text = self.i18n.login.number;
				}else if(self.pwd.length < 6){
					text = self.i18n.reg.pass;
				}else if(self.pwd2.length != 6){
					text = self.i18n.reg.six;
				}else if(self.wallet_address.length < 30 || self.wallet_address.length > 45){
					text = self.i18n.reg.wallet;
				}else if (self.imageSrc == '') {
					text = self.i18n.reg.upload;
				}
				if(text != ''){
					uni.showToast({
						icon: 'none',
						title: text
					});
				}else{
					const uploadTask = uni.uploadFile({
						url : service.api.reg.help+'?token='+self.token+'&lang='+uni.getStorageSync('language'),
						filePath: self.imageSrc,
						name: 'file',
						formData: {
							"realname":self.realname,
							/* "engname":self.engname, */
							"account":self.account,
							"pwd":self.pwd,
							'pwd2':self.pwd2,
							"wallet_address":self.wallet_address,
						},
						success: function (res) {
							res = res.data;
							res = eval('(' + res + ')');
							uni.showToast({
								icon: 'none',
								title: res.info
							});
							if(res.code == 1){
								setTimeout(function(){
									uni.navigateBack({
										delta: 1
									});
								}, 1000);
							}else{
								self.status = 1;
							}
						}
					});
				}
			}
        }
    }
</script>
